
package com.starshineworks.util.actions;

import com.starshineworks.entities.EntityE;
import com.starshineworks.entities.TeamE;
import com.starshineworks.entities.UserE;
import com.starshineworks.util.Util;
//import com.starshineworks.util.FileUploader;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionCreateTeam extends Action {  
       
    @Override
    public boolean perform(HttpServletRequest  request,
                           HttpServletResponse response,
                           HttpServlet         serv,
                           boolean             isMultiPart)
    {   
        Map<String,String[]> map = request.getParameterMap();
        HttpSession sess = request.getSession();
        try {
            Integer uId = (Integer) sess.getAttribute("uId");
            Integer currId = Integer.parseInt(map.get("currentid")[0]);
            boolean isNew = currId == -1;
            Integer tId = null;
            if (isNew) {
                tId = EntityE.create("TeamE", map);
                // Must update manager after creation because map from form is
                // immutable.
                List<EntityE> list = EntityE.retrieve(new TeamE(tId));
                TeamE t = (TeamE) list.get(0);//TeamE.retrieve(new TeamE(tId)).get(0);
                Map<String, String[]> m = new HashMap<String, String[]>();
                String[] mg = {uId.toString(), null, null, null};
                m.put("_manager", mg);
                TeamE.updateObjectFields(t, m);
            } else {
                EntityE.updateObjectFields(new TeamE(currId), map);
            }
        } catch (Exception e) {
            Util.handleException(e);
            return false;
        }
        return true;
    }
    
    @Override
    public String getNextPage() {
        return "CreateTeams";
    }

    @Override
    public String getError() {
        return "Team info cannot be updated or imported.";
    }

}
